//全部菜单变量
var allMenus = new Array();
/**
 * 添加常用菜单接口调用
 * @param menu 菜单完整对象
 * @param successCallback 成功后回调函数
 */
function addCustomMenu(menu , successCallback) {
    $.post(MENUUSER_SERVICE_URL + 'customMenu/addCustomMenu', {menuCode: menu.uumsFunction.functionCode}, function (data) {
        if (data.status == 'SUCCESS') {
            if (successCallback) {
                successCallback();
                customMenuInit();
                customMenuUseLog(menu.uumsFunction,"Y");
                //同步常用菜单状态
                syncCustomMenu(ACTION_ADD,menu);
            }
        } else if (data.status == 'FAILURE') {
            Ext.Msg.alert("操作失败 !", data.body);
        }
    });
}

/**
 * 取消常用菜单接口调用
 * @param menu 菜单完整对象
 * @param successCallback 成功后回调函数
 */
function delCustomMenu(menu, successCallback) {
    $.post(MENUUSER_SERVICE_URL + 'customMenu/delCustomMenu', {menuCode: menu.uumsFunction.functionCode}, function (data) {
        if (data.status == 'SUCCESS') {
            if (successCallback) {
                successCallback();
                customMenuInit();
                customMenuUseLog(menu.uumsFunction,"N");
                //同步常用菜单状态
                syncCustomMenu(ACTION_DEL,menu);
            }
        } else if (data.status == 'FAILURE') {
            Ext.Msg.alert("操作失败 !", data.body);
        }
    });
}

/**
 * 修改常用菜单序列
 */
function updateCustomMenuSequence(direction,currentMenuCode,correlationMenuCode,successCallback){
    $.post(MENUUSER_SERVICE_URL + "customMenu/updateCustomMenuSequence",{"direction":direction,"currentMenuCode":currentMenuCode,"correlationMenuCode":correlationMenuCode},function(data){
        if(data.status == "SUCCESS" && data.body == 1){
            if(successCallback){
                successCallback();
            }
        }else {
            Ext.Msg.alert("操作失败 !", data.body);
        }
    });
}

/**
 * 后台菜单查询接口（从所有菜单中根据菜单名称关键字匹配）
 * @param keyword 菜单关键字
 * @param successCallback 成功后回调函数
 */
/*function queryMenus(keyword, successCallback) {
    $.post(MENUCORE_SERVICE_URL + "indexMenu/queryMenus", {keyword: keyword}, function (data) {
        if (data.status == 'SUCCESS') {
            if (successCallback) {
                successCallback(data.body);
            }
        }
    });
}*/

/**
 * js菜单查询通过关键字（从所有菜单中根据菜单名称关键字匹配）
 * @param keyword 菜单关键字
 * @param successCallback 成功后回调函数
 */
function queryMenus(keyword, successCallback){
	var len = allMenus.length;
	var menusOfQuery = [];
	//输入空格后keyword.trim()为"",防止indexOf匹配
	if(keyword.trim() !== ""){
		for(var i=0;i<len;i++){
			if(allMenus[i].rootFunctionCode != null){
				//根据关键字来匹配
				if(allMenus[i].uumsFunction.functionName.indexOf(keyword.trim()) >= 0){
					menusOfQuery.push(allMenus[i]);
					if(menusOfQuery.length >= 20){
						break;
					}
				}
			}
		}
	}
	//根据菜单长度从小到大排序
	menusOfQuery.sort(function(a,b){
		return a.uumsFunction.functionName.length - b.uumsFunction.functionName.length;
	});
	if (successCallback) {
        successCallback(menusOfQuery);
    }
}

/**
 * 绑定菜单快捷键
 * @param menuCode 菜单编码
 * @param keyboardAscii 快捷键编码
 * @param successCallback 成功后回调函数
 */
function bindMenuHotKey(menuCode, keyboardAscii, successCallback) {
    $.post(CUSTOM_SERVICE_URL + "menuHotKey/bindMenuHotKey", {
        menuCode: menuCode,
        keyboardAscii: keyboardAscii
    }, function (data) {
        if (data.status == 'SUCCESS') {
            if (successCallback) {
                successCallback(data.body);
            }
        } else if (data.status == 'FAILURE') {
            Ext.Msg.alert("操作失败 !", data.body);
        }
    });
}

/**
 * 快捷键解绑
 */
function unbindMenuHotKey(keyboardAscii, successCallback) {
    $.post(CUSTOM_SERVICE_URL + "menuHotKey/unbindMenuHotKey", {keyboardAsccii: keyboardAscii}, function (data) {
        if (data.status == "SUCCESS") {
            if (successCallback) {
                successCallback(data.body);
            }
        } else if (data.status == 'FAILURE') {
            Ext.Msg.alert("操作失败 !", data.body);
        }
    });
}

$(function () {
    // 所有菜单以及常用菜单渲染支持 收藏与取消收藏渲染支持 快捷键支持
    var $homeMenuTree = $('#menu'), metisMenuDataKey = 'metisMenu', menuCodeCustomMenu = {}, menuCodeMenuLeafs = {},
        asciiUumsFunction = {};

    // 添加常用菜单的Document操作方法
    var addCustomMenuDomOpt = function (uapMenuVo) {
        var uumsFunction = uapMenuVo.uumsFunction;
        // 添加已收藏样式
        $.each(menuCodeMenuLeafs[uumsFunction.functionCode], function (index, item) {
            item.addClass('save');
        });

        // 创建点击事件
        var customMenuClickFunction = function () {
            openTab(
                uumsFunction.functionName,
                uumsFunction.functionUrl,
                uumsFunction.pageEmbedType,
                uumsFunction.functionCode,
                uumsFunction.sourceSystem,
                uumsFunction.parentFunction,
                CUSTOM_MENU_ACCESS
            );
        }

        // 添加所有菜单中常用菜单
        var customMenuLeafHtml = '<li><a class="toggle">{functionName}</a></li>';
        var $customMenuLeaf = $(
            customMenuLeafHtml.replace('{functionName}', uumsFunction.functionName)
        );

        $customMenuLeaf.attr({"menuCode":uumsFunction.functionCode});
        $customMenuLeaf.click(customMenuClickFunction);
        $("#menu").find('.custem-menu').append($customMenuLeaf);

        // 添加首页菜单中常用菜单
        var customMenuHomeHtml =
            '<div class="custom-menu-list" onclick="openTab(\'{functionName}\', \'{functionUrl}\', \'{pageEmbedType}\', \'{functionCode}\', \'{sourceSystem}\',\'{parentFunction}\',\'{accessPath}\')">' +
            '<ul>' +
            '<li class="customMenu_img"><img src="{menuIconSrc}"></li>' +
            '<li class="customMenu_name">{functionName}</li>' +
            '</ul>' +
            '</div>';

        var rootMenuCode = uapMenuVo.rootFunctionCode;
        if (!rootMenuCode && uapMenuVo.customMenu) {
            rootMenuCode = uapMenuVo.customMenu.rootCode;
        }

        var $customMenuHome = $(
            customMenuHomeHtml.replace('{functionName}', uumsFunction.functionName)
                .replace('{functionUrl}', uumsFunction.functionUrl)
                .replace('{pageEmbedType}', uumsFunction.pageEmbedType)
                .replace('{functionCode}', uumsFunction.functionCode)
                .replace('{sourceSystem}', uumsFunction.sourceSystem)
                .replace('{menuIconSrc}', CUSTOM_MENU_ICON_MAPPING[rootMenuCode] || 'static/resources/img/默认图标.png')
                .replace('{functionName}', uumsFunction.functionName)
                .replace('{parentFunction}',uumsFunction.parentFunction)
                .replace('{accessPath}',CUSTOM_MENU_ACCESS)
        );

        $customMenuHome.attr({"menuCode":uumsFunction.functionCode});
        $('#customMenuHome').append($customMenuHome);

        // 添加 系统设置-常用菜单设置 中 常用菜单
        var $customMenuSetting = $("<li class='leaf_li'><label>{functionName}</label><span class='icon-btn delete'></span><span class='icon-btn down'></span><span class='icon-btn up'></span></li>".replace("{functionName}", uumsFunction.functionName));
        $customMenuSetting.attr({"menuCode":uumsFunction.functionCode});
        $(".selected-menu-box").append($customMenuSetting);
        //绑定取消常用菜单事件
        $customMenuSetting.find("span.delete").click(function () {
            delCustomMenu(uapMenuVo, function () {
                delCustomMenuDomOpt(uapMenuVo);
                //删除搜索历史列表菜单对应Save状态
                $.each($("#customMenuSetting .query-clean-box .query-results-list li.save"),function(index,item){
                    if($(item).find("label").text() == uumsFunction.functionName){
                        $(item).removeClass("save");
                    }
                })
            });
        });

        //绑定修改常用菜单序列事件
        $customMenuSetting.find("span.up").click(function(){
            var correlationMenuCode = $(".selected-menu-box li[menuCode=" + uumsFunction.functionCode + "]").prev().attr("menuCode");
            updateCustomMenuSequence("Y",uumsFunction.functionCode,correlationMenuCode,function(){
                updateCustomMenuSequenceDomOpt("Y",uumsFunction.functionCode,correlationMenuCode);
            });
        });

        //绑定修改常用菜单序列事件
        $customMenuSetting.find("span.down").click(function(){
            var correlationMenuCode = $(".selected-menu-box li[menuCode=" + uumsFunction.functionCode + "]").next().attr("menuCode");
            updateCustomMenuSequence("N",uumsFunction.functionCode,correlationMenuCode,function(){
                updateCustomMenuSequenceDomOpt("N",uumsFunction.functionCode,correlationMenuCode);
            });
        });

        menuCodeCustomMenu[uumsFunction.functionCode] = [$customMenuLeaf, $customMenuHome, $customMenuSetting];
    };

    // 删除常用菜单的Document操作方法
    var delCustomMenuDomOpt = function (uapMenuVo) {
        var uumsFunction = uapMenuVo.uumsFunction;
        // 删除已收藏样式
        $.each(menuCodeMenuLeafs[uumsFunction.functionCode], function (index, item) {
            item.removeClass('save');
        });
        // 清理DOM
        if (menuCodeCustomMenu.hasOwnProperty(uumsFunction.functionCode)) {
            $.each(menuCodeCustomMenu[uumsFunction.functionCode], function (index, item) {
                item.remove();
            })
            delete menuCodeCustomMenu[uumsFunction.functionCode];
        }
    }

    //更新常用菜单列表序列
    var updateCustomMenuSequenceDomOpt = function(direction,currentMenuCode,correlationMenuCode){
        //首页菜单列表
        var listCurrentCustomMenu = $("#menu").find(".custem-menu li[menuCode=" + currentMenuCode +"]");
        var listCorrelationMenu = $("#menu").find(".custem-menu li[menuCode=" + correlationMenuCode +"]");

        //常用菜单设置
        var settingCurrentCustomMenu = $(".selected-menu-box li[menuCode=" + currentMenuCode + "]");
        var settingCorrelationMenu = $(".selected-menu-box li[menuCode=" + correlationMenuCode + "]");
        //首页版本常用菜单
        var indexCurrentCustomMenu = $("#customMenuHome div[menuCode=" + currentMenuCode + "]");
        var indexCorrelationMenu = $("#customMenuHome div[menuCode=" + correlationMenuCode + "]");


        if( "Y" == direction ){
            settingCurrentCustomMenu.insertBefore(settingCorrelationMenu);
            indexCurrentCustomMenu.insertBefore(indexCorrelationMenu);
            listCurrentCustomMenu.insertBefore(listCorrelationMenu);
        }else{
            settingCurrentCustomMenu.insertAfter(settingCorrelationMenu);
            indexCurrentCustomMenu.insertAfter(indexCorrelationMenu);
            listCurrentCustomMenu.insertAfter(listCorrelationMenu);
        }
    }

    // 初始化首页所有菜单和常用菜单
    // var initHomePageIndexMenuCustomMenu = function (uapMenuVos) {
    //     var rootHtml =
    //         '<li class="menu-firstList">' +
    //         '<a class="has-arrow toggle toggle-first">' +
    //         '<img src="{menuIconSrc}">' +
    //         '<label>{functionName}</label>' +
    //         '</a>' +
    //         '<ul class="collapse"></ul>' +
    //         '</li>';
    //
    //     var dirHtml =
    //         '<li class="menu-subList">' +
    //         '<a class="toggle toggle-subList">' +
    //         '<label>{functionName}</label>' +
    //         '</a>' +
    //         '<ul class="leaf-list collapse"></ul>' +
    //         '</li>';
    //
    //     var leafHtml =
    //         '<li class="leaf_li col-md-4" title="{functionName}">' +
    //         '<span class="star"></span>' +
    //         '<label>{functionName}</label>' +
    //         '<span>{hotKey}</span>' +
    //         '</li>';
    //
    //     $.each(uapMenuVos, function (rootIndex, rootItem) {   // 三层循环构建首页所有菜单按钮
    //         if (rootItem.uumsFunction.parentFunction == '0') {
    //             var menuIconSrc = INDEX_MENU_ICON_MAPPING[rootItem.uumsFunction.functionCode] || 'resources/img/menu/index/ddgl.png';
    //             var $root = $(
    //                 rootHtml.replace('{menuIconSrc}', menuIconSrc)
    //                     .replace('{functionName}', rootItem.uumsFunction.functionName)
    //             );
    //             $homeMenuTree.append($root);
    //             var $dirParent = $root.children('ul');
    //
    //             $.each(uapMenuVos, function (dirIndex, dirItem) {
    //                 if (dirItem.uumsFunction.parentFunction == rootItem.uumsFunction.functionCode) {
    //                     var $dir = $(dirHtml.replace('{functionName}', dirItem.uumsFunction.functionName));
    //                     $dirParent.append($dir);
    //                     var $leafParent = $dir.children('ul');
    //                     $.each(uapMenuVos, function (leafIndex, leafItem) {
    //                         if (leafItem.uumsFunction.parentFunction == dirItem.uumsFunction.functionCode) {
    //                             // 构建快捷键展示文字
    //                             var hotKey = toggleKeyNameOrAscii(leafItem.menuHotKey ? leafItem.menuHotKey.keyboardAscii : '');
    //                             var $leaf = $( // 创建子节点对象
    //                                 leafHtml.replace('{functionName}', leafItem.uumsFunction.functionName)
    //                                     .replace('{functionName}', leafItem.uumsFunction.functionName)
    //                                     .replace('{hotKey}', hotKey)
    //                             );
    //
    //                             $leaf.attr("functionCode", leafItem.uumsFunction.functionCode);
    //
    //                             if (leafItem.customMenu && leafItem.customMenu.id) { // 添加已收藏常用菜单样式
    //                                 $leaf.addClass('save');
    //                             }
    //
    //                             // 存储所有菜单叶子节点CODE与DOM关系 其他收藏取消收藏事件中需要使用
    //                             if (menuCodeMenuLeafs[leafItem.uumsFunction.functionCode]) {
    //                                 menuCodeMenuLeafs[leafItem.uumsFunction.functionCode].push($leaf);
    //                             } else {
    //                                 menuCodeMenuLeafs[leafItem.uumsFunction.functionCode] = [$leaf];
    //                             }
    //
    //                             var $collectClickDom = $leaf.find('.star'); // 构建收藏事件和样式操作
    //                             var collectClickFunction = function () {
    //                                 $collectClickDom.off('click', collectClickFunction);
    //                                 if ($leaf.hasClass('save')) {
    //                                     delCustomMenu(leafItem, function () {
    //                                         $collectClickDom.on('click', collectClickFunction);
    //                                         delCustomMenuDomOpt(leafItem);
    //                                     });
    //                                 } else {
    //                                     addCustomMenu(leafItem, function () {
    //                                         $collectClickDom.on('click', collectClickFunction);
    //                                         addCustomMenuDomOpt(leafItem);
    //                                     });
    //                                 }
    //                             }
    //
    //                             $collectClickDom.click(collectClickFunction); // 收藏事件添加
    //
    //                             // 绑定事件
    //                             $leaf.children('label').click(function () {
    //                                 openTab(
    //                                     leafItem.uumsFunction.functionName,
    //                                     leafItem.uumsFunction.functionUrl,
    //                                     leafItem.uumsFunction.pageEmbedType,
    //                                     leafItem.uumsFunction.functionCode,
    //                                     leafItem.uumsFunction.sourceSystem,
    //                                     leafItem.uumsFunction.parentFunction,
    //                                     INDEX_MENU_ACCESS
    //
    //                                 );
    //                             });
    //                             // 渲染dom
    //                             $leafParent.append($leaf);
    //                         }
    //                     });
    //                 }
    //             });
    //         }
    //     });
    //     if ($homeMenuTree.data(metisMenuDataKey)) {
    //         $homeMenuTree.metisMenu('dispose');
    //     }
    //     $homeMenuTree.metisMenu();
    // }

    // 初始化常用菜单设置所有菜单树
    // var initCustomMenuSettingsIndexMenu = function (uapMenuVos) {
    //     var $customMenuTree = $('#customMenuSetting').find('.tree-box,.metismenu');
    //     var rootHtml =
    //         '<li class="parent-li">' +
    //         '<a class="toggle" href="#">' +
    //         '<label>{functionName}</label>' +
    //         '</a>' +
    //         '<ul></ul>'
    //     '</li>';
    //     var dirHtml =
    //         '<li class="parent-li">' +
    //         '<a class="toggle" href="#">' +
    //         '<label>{functionName}</label>' +
    //         '</a>' +
    //         '<ul class="leaf-list"></ul>' +
    //         '</li>';
    //     var leafHtml =
    //         '<li class="leaf_li">' +
    //         '<label>{functionName}</label>' +
    //         '<span class="icon-btn add" />' +
    //         '</li>';
    //     $.each(uapMenuVos, function (rootIndex, rootItem) {   // 三层循环构建所有菜单按钮
    //         if (rootItem.uumsFunction.parentFunction == '0') {
    //             var $root = $(rootHtml.replace('{functionName}', rootItem.uumsFunction.functionName));
    //             $customMenuTree.append($root);
    //             var $dirParent = $root.children('ul');
    //             $.each(uapMenuVos, function (dirIndex, dirItem) {
    //                 if (dirItem.uumsFunction.parentFunction == rootItem.uumsFunction.functionCode) {
    //                     var $dir = $(dirHtml.replace('{functionName}', dirItem.uumsFunction.functionName));
    //                     $dirParent.append($dir);
    //                     var $leafParent = $dir.children('ul');
    //                     $.each(uapMenuVos, function (leafIndex, leafItem) {
    //                         if (leafItem.uumsFunction.parentFunction == dirItem.uumsFunction.functionCode) {
    //                             // 创建子节点对象
    //                             var $leaf = $(leafHtml.replace('{functionName}', leafItem.uumsFunction.functionName));
    //
    //                             if (leafItem.customMenu && leafItem.customMenu.id) { // 添加已收藏常用菜单样式
    //                                 $leaf.addClass('save');
    //                             }
    //
    //                             // 存储所有菜单叶子节点CODE与DOM关系 其他收藏取消收藏事件中需要使用
    //                             if (menuCodeMenuLeafs[leafItem.uumsFunction.functionCode]) {
    //                                 menuCodeMenuLeafs[leafItem.uumsFunction.functionCode].push($leaf);
    //                             } else {
    //                                 menuCodeMenuLeafs[leafItem.uumsFunction.functionCode] = [$leaf];
    //                             }
    //
    //                             var collectClickFunction = function () { // 构建收藏事件和样式操作
    //                                 $leaf.find("span").off('click', collectClickFunction);
    //                                 if ($leaf.hasClass('save')) {
    //                                     delCustomMenu(leafItem, function () {
    //                                         $leaf.find("span").on('click', collectClickFunction);
    //                                         delCustomMenuDomOpt(leafItem);
    //                                     });
    //                                 } else {
    //                                     addCustomMenu(leafItem, function () {
    //                                         $leaf.find("span").on('click', collectClickFunction);
    //                                         addCustomMenuDomOpt(leafItem);
    //                                     });
    //                                 }
    //                             }
    //                             $leaf.find("span").click(collectClickFunction);   // 收藏事件添加
    //
    //                             // 渲染dom
    //                             $leafParent.append($leaf);
    //                         }
    //                     });
    //                 }
    //             });
    //         }
    //     });
    //     if ($customMenuTree.data(metisMenuDataKey)) {
    //         $customMenuTree.metisMenu('dispose');
    //     }
    //     $customMenuTree.metisMenu();
    // }

    // 初始化快捷键所有菜单树
    // var initHotKeySettingsIndexMenu = function (uapMenuVos) {
    //     var rootHtml =
    //         '<li class="parent-li">' +
    //         '<a class="toggle">' +
    //         '<label>{functionName}</label>' +
    //         '</a>' +
    //         '<ul></ul>' +
    //         '</li>';
    //
    //     var dirHtml =
    //         '<li class="parent-li">' +
    //         '<a class="toggle">' +
    //         '<label>{functionName}</label>' +
    //         '</a>' +
    //         '<ul class="leaf-list"></ul>' +
    //         '</li>';
    //
    //     var leafHtml =
    //         ' <li class="leaf_li">' +
    //         '<label>{functionName}</label>' +
    //         '</li>';
    //
    //     $('.hot-key-value').each(function (index, item) {
    //         var $hotKeyMenuTree = $(item).find('.default-list').find('ul');
    //         $.each(uapMenuVos, function (rootIndex, rootItem) {
    //             if (rootItem.uumsFunction.parentFunction == '0') {
    //                 var $root = $(rootHtml.replace('{functionName}', rootItem.uumsFunction.functionName));
    //                 $hotKeyMenuTree.append($root);
    //                 var $dirParent = $root.children('ul');
    //                 $.each(uapMenuVos, function (dirIndex, dirItem) {
    //                     if (dirItem.uumsFunction.parentFunction == rootItem.uumsFunction.functionCode) {
    //                         var $dir = $(dirHtml.replace('{functionName}', dirItem.uumsFunction.functionName));
    //                         $dirParent.append($dir);
    //                         var $leafParent = $dir.children('ul');
    //                         $.each(uapMenuVos, function (leafIndex, leafItem) {
    //                             if (leafItem.uumsFunction.parentFunction == dirItem.uumsFunction.functionCode) {
    //                                 // 创建子节点对象
    //                                 var $leaf = $(leafHtml.replace('{functionName}', leafItem.uumsFunction.functionName));
    //                                 $leaf.click(function () {
    //                                     var keyboardAscii = toggleKeyNameOrAscii($(item).prev().prop('id'));
    //                                     bindMenuHotKey(leafItem.uumsFunction.functionCode, keyboardAscii, function () {
    //                                         asciiUumsFunction[keyboardAscii] = leafItem.uumsFunction;
    //                                         $(item).find('.tree-query-box').slideUp("fast");
    //                                         //修改菜单快捷键标识符
    //                                         $(".leaf_li[functioncode=" + leafItem.uumsFunction.functionCode + "]").find("span:eq(1)").html($(item).prev().prop('id'));
    //                                         drawHotKeyInfo({
    //                                             "functionName": leafItem.uumsFunction.functionName,
    //                                             "functionCode": leafItem.uumsFunction.functionCode,
    //                                             "keyboardAscii": keyboardAscii
    //                                         }, $(item).find(".tree-control"));
    //                                     });
    //                                 });
    //                                 // 渲染dom
    //                                 $leafParent.append($leaf);
    //                             }
    //                         });
    //                     }
    //                 });
    //             }
    //         });
    //         if ($hotKeyMenuTree.data(metisMenuDataKey)) {
    //             $hotKeyMenuTree.metisMenu('dispose');
    //         }
    //         $hotKeyMenuTree.metisMenu();
    //     });
    //
    // }

    // 构建首页Content的常用菜单 用户设置菜单树
    // $.get(MENUUSER_SERVICE_URL + 'customMenu/queryCustomMenus', function (data) {  // 构建常用菜单
    //     if (data.status == 'SUCCESS') {
    //         $.each(data.body, function (index, item) {
    //             addCustomMenuDomOpt(item);
    //         });
    //         if ($homeMenuTree.data(metisMenuDataKey)) {
    //             $homeMenuTree.metisMenu('dispose');
    //         }
    //         $homeMenuTree.metisMenu();
    //     }
    // });

    // 构建所有菜单和常用菜单 快捷键绑定树
    // $.get(MENUCORE_SERVICE_URL + 'indexMenu/queryMenus', function (data) {
    //     if (data.status == 'SUCCESS') {
    //     	//491266 创建全局菜单数组方便查询菜单
    //         allMenus = data.body;
    //         initHomePageIndexMenuCustomMenu(data.body);
    //         initCustomMenuSettingsIndexMenu(data.body);
    //         initHotKeySettingsIndexMenu(data.body);
    //     } else {
    //         console.error(data.body);
    //         window.location.href = UAP_SERVER_NAME + 'error.html';
    //     }
    // });

    // 初始化快捷键功能 生成快捷键设置Document展示
    $.get(CUSTOM_SERVICE_URL + 'menuHotKey/queryMenuHotKeys', function (data) {
        if (data.status == 'SUCCESS') {
            // 构建快键键KV键值对 渲染快捷键界面
            $.each(data.body, function (index, item) {
                asciiUumsFunction[item.menuHotKey.keyboardAscii] = item.uumsFunction;
                var $treeControl = $('#' + toggleKeyNameOrAscii(item.menuHotKey.keyboardAscii)).next().find(".tree-control");
                $treeControl.find('span').html(item.uumsFunction.functionName);

                //解绑事件
                drawHotKeyInfo({
                    "functionName": item.uumsFunction.functionName,
                    "functionCode": item.uumsFunction.functionCode,
                    "keyboardAscii": item.menuHotKey.keyboardAscii
                }, $treeControl);
            });

            // 绑定快捷键
            $(window).keydown(function (event) {
                if (asciiUumsFunction[event.keyCode]) {
                    openTab(
                        asciiUumsFunction[event.keyCode].functionName,
                        asciiUumsFunction[event.keyCode].functionUrl,
                        asciiUumsFunction[event.keyCode].pageEmbedType,
                        asciiUumsFunction[event.keyCode].functionCode,
                        asciiUumsFunction[event.keyCode].sourceSystem,
                        asciiUumsFunction[event.keyCode].parentFunction,
                        HOT_KEY_ACCESS
                    );

                    event.preventDefault(); // 阻止按键原功能生效
                }
            });
        }
    });

    /**
     * 将快捷键输入框替换成文本显示
     * @param obj
     * @param treeControl
     */
    function drawHotKeyInfo(obj, treeControl) {
        //清空原始树输入框信息
        treeControl.find("span").html("");
        var hotKeyInfo = $("<div class='hotKeyInfo'><span></span><label><img src='resources/img/cross-sm.png'/></label></div>");
        hotKeyInfo.find("span").html(obj.functionName);
        hotKeyInfo.find("label").click(function () {
            unbindMenuHotKey(obj.keyboardAscii, function () {
                //移除快捷键绑定对象
                delete asciiUumsFunction[obj.keyboardAscii];
                treeControl.removeClass("active");
                hotKeyInfo.replaceWith(treeControl);
                //移除菜单快捷键标识符
                $(".leaf_li[functioncode=" + obj.functionCode + "]").find("span:eq(1)").html("");
            });
        });
        //替换
        treeControl.replaceWith(hotKeyInfo);
    }

    // 首页所有菜单搜索事件绑定
    $('#menu-search-input').bind('input propertychange', function () {
    	var $menuSearchInput = $(this);
    	var $resultContainer = $menuSearchInput.parents('.query-clean-box') .find('.query-results-list');
    	$resultContainer.html(''); // 搜索前清空上次搜索结果
        var keyword = $menuSearchInput.val();
        if(fuzzySearchRegex.test(keyword)){
        	 $resultContainer.html('<div class="no-result">搜索不到您需要的结果 !</div>');
        }else if ( !fuzzySearchRegex.test(keyword) && keyword.length >= 2) {
            queryMenus(keyword, function (uapMenuVos) {
                var resultHtml =
                    '<li>' +
                    '<span class="star"></span>' +
                    '<label>{functionName}</label>' +
                    '<span>{hotKey}</span>' +
                    '</li>';
                if (!uapMenuVos || uapMenuVos.length == 0) {
                    $resultContainer.html('<div class="no-result">搜索不到您需要的结果 !</div>');
                }

                $.each(uapMenuVos, function (index, item) {
                    //只显示前20个列表信息
                    if (index >= 20) {
                        return;
                    }

                    // 构建快捷键展示文字
                    var hotKey = toggleKeyNameOrAscii(item.menuHotKey ? item.menuHotKey.keyboardAscii : '');
                    var $resultHtml = $(
                        resultHtml.replace('{functionName}', item.uumsFunction.functionName)
                            .replace('{hotKey}', hotKey)
                    );

                    $resultHtml.data("item", item);

                    if (item.customMenu && item.customMenu.id) { // 添加已收藏常用菜单样式
                        $resultHtml.addClass('save');
                    }

                    var $collectClickDom = $resultHtml.find('.star');
                    var collectClickFunction = function () { // 构建收藏事件和样式操作
                        $collectClickDom.off('click', collectClickFunction);
                        if ($resultHtml.hasClass('save')) {
                            delCustomMenu(item, function () {
                                $resultHtml.removeClass('save');
                                $collectClickDom.on('click', collectClickFunction);
                                delCustomMenuDomOpt(item);
                            });
                        } else {
                            addCustomMenu(item, function () {
                                $resultHtml.addClass('save');
                                $collectClickDom.on('click', collectClickFunction);
                                addCustomMenuDomOpt(item);
                            });
                        }
                    }
                    $collectClickDom.click(collectClickFunction);   // 收藏事件添加

                    // 绑定事件
                    $resultHtml.children('label').click(function () {
                        openTab(
                            item.uumsFunction.functionName,
                            item.uumsFunction.functionUrl,
                            item.uumsFunction.pageEmbedType,
                            item.uumsFunction.functionCode,
                            item.uumsFunction.sourceSystem,
                            item.uumsFunction.parentFunction,
                            INDEX_MENU_ACCESS
                        );
                    });

                    // 渲染dom
                    $resultContainer.append($resultHtml);
                });

                /** 添加悬浮 菜单层级提示 **/
                var $hint = $(
                    "<div class='hint'>" +
                    "<p class='hint-content'><img class='tip-icon' src='resources/img/menu/index/ddgl.png'/>" +
                    "<span></span>" +
                    "－" +
                    "<span></span></p><img class='tip-bg' src='resources/img/dxb.png'>" +
                    "</div>");

                $resultContainer.find("li").hover(function () {
                    var item = $(this).data("item");
                    $hint.find("span:eq(0)").html(item.rootFunctionName);
                    $hint.find("span:eq(1)").html(item.subFunctionName);
                    $hint.children("p").find("img").attr("src", INDEX_MENU_ICON_MAPPING[item.rootFunctionCode]);
                    $(this).append($hint);
                }, function () {
                    $hint.remove();
                });

            });
        }
    });

    // 常用菜单设置搜索事件绑定
    $('#customMenuSetting').find('.custom-men-search').find('input').bind('input propertychange', function () {
    	var $menuSearchInput = $(this);
    	var $resultContainer = $menuSearchInput.parents('.query-clean-box').find('.query-results-list');
    	$resultContainer.html(''); // 搜索前清空上次搜索结果
        var keyword = $menuSearchInput.val();
        if(fuzzySearchRegex.test(keyword)){
        	$resultContainer.html('<div class="no-result">搜索不到您需要的结果 !</div>');
        }else if ( !fuzzySearchRegex.test(keyword) && keyword.length >= 2) {
            queryMenus(keyword, function (uapMenuVos) {

                var resultHtml =
                    '<li class="leaf_li">' +
                    '<label>{functionName}</label>' +
                    '<span class="icon-btn add" />' +
                    '</li>';

                if (!uapMenuVos || uapMenuVos.length == 0) {
                    $resultContainer.html('<div class="no-result">搜索不到您需要的结果 !</div>');
                }

                $.each(uapMenuVos, function (index, item) {
                    // 构建快捷键展示文字
                    var $resultHtml = $(resultHtml.replace('{functionName}', item.uumsFunction.functionName));
                    if (item.customMenu && item.customMenu.id) { // 添加已收藏常用菜单样式
                        $resultHtml.addClass('save');
                    }
                    var collectClickFunction = function () { // 构建收藏事件和样式操作
                        if (!$resultHtml.hasClass('save')) {
                            addCustomMenu(item, function () {
                                $resultHtml.addClass('save');
                                addCustomMenuDomOpt(item);
                            });
                        }
                    }

                    $resultHtml.find("span.icon-btn.add").click(collectClickFunction);   // 收藏事件添加

                    // 渲染dom
                    $resultContainer.append($resultHtml);
                });
            });
        }
    });

    // 快捷键设置搜索事件绑定
    $('.hot-key-value').each(function (index, item) {
        $(item).find('.query-box').find('input').bind('input propertychange', function () {
            var $menuSearchInput = $(this);
            var $resultContainer = $menuSearchInput.parents('.query-clean-box').find('.query-results-list');
            $resultContainer.html(''); // 搜索前清空上次搜索结果
            var keyword = $menuSearchInput.val();
            if(fuzzySearchRegex.test(keyword)){
            	$resultContainer.html('<div class="no-result">搜索不到您需要的结果 !</div>');
            }else if ( !fuzzySearchRegex.test(keyword) && keyword.length >= 2) {
                queryMenus(keyword, function (uapMenuVos) {
                    var resultHtml =
                        ' <li class="leaf_li">' +
                        '<label>{functionName}</label>' +
                        '</li>';
                    if (!uapMenuVos || uapMenuVos.length == 0) {
                        $resultContainer.html('<div class="no-result">搜索不到您需要的结果 !</div>');
                    }
                    $.each(uapMenuVos, function (index, leafItem) {
                        // 构建快捷键展示文字
                        var $resultHtml = $(resultHtml.replace('{functionName}', leafItem.uumsFunction.functionName));
                        // 构建搜索结果点击事件
                        $resultHtml.click(function () {
                            var keyboardAscii = toggleKeyNameOrAscii($(item).prev().prop('id'));
                            bindMenuHotKey(leafItem.uumsFunction.functionCode, keyboardAscii, function () {
                                $(item).find('.tree-query-box').slideUp("fast");
                                asciiUumsFunction[keyboardAscii] = leafItem.uumsFunction;
                                //修改菜单快捷键标识符
                                $(".leaf_li[functioncode=" + leafItem.uumsFunction.functionCode + "]").find("span:eq(1)").html($(item).prev().prop('id'));
                                drawHotKeyInfo({
                                    "functionName": leafItem.uumsFunction.functionName,
                                    "functionCode": leafItem.uumsFunction.functionCode,
                                    "keyboardAscii": keyboardAscii
                                }, $(item).find(".tree-control"));
                            });
                        });
                        // 渲染dom
                        $resultContainer.append($resultHtml);
                    });
                });
            }
        });
    });

});
